package com.finanzaspersonales;

import java.sql.Date;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import clases.EF;
import clases.SubRubro;
import clases.tipoEF;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class comparacionEF extends Activity implements OnItemSelectedListener {
	private comparacionEF me=this;
	private String idUsuario;
	private Button btn_comparar;
	private double monto;
	private int cuota;
	private int posicion1;
	private int posicion2;
	private int posicion3;
	private int posicion4;
	private int idTipoEF1;
	private int idTipoEF2;
	private int idEF1;
	private int idEF2;
	double dblnum1=0.0;
	double dblnum2=0.0;
	private ArrayList<tipoEF> tipoef = new ArrayList<tipoEF>();
	private ArrayList<EF> ef1 = new ArrayList<EF>();
	private ArrayList<EF> ef2 = new ArrayList<EF>();
	ArrayList<String> nomTipoEF = new ArrayList<String>();
	ArrayList<String> nomEF1 = new ArrayList<String>();
	ArrayList<String> nomEF2 = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		idUsuario= this.getIntent().getExtras().getString("idUsuario");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comparacion_ef);
		
		Spinner spn_tipoef1 = (Spinner)findViewById(R.id.spnTipoEF1);
		Spinner spn_ef1 = (Spinner)findViewById(R.id.spnEF1);
		Spinner spn_tipoef2 = (Spinner)findViewById(R.id.spnTipoEF2);
		Spinner spn_ef2 = (Spinner)findViewById(R.id.spnEF2);
		spn_tipoef1.setOnItemSelectedListener(this);
		spn_tipoef2.setOnItemSelectedListener(this);
		tipoef = llenaTipoEF();
		for(int i=0; i<tipoef.size();i++){
			nomTipoEF.add(tipoef.get(i).getNombre());
		}

		//		llenar el spinner con ese arreglo
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, nomTipoEF);		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spn_tipoef1.setAdapter(adapter);
	    spn_tipoef2.setAdapter(adapter);
  	    
		btn_comparar = (Button) findViewById(R.id.btnCompararEF);
		btn_comparar.setOnClickListener(new View.OnClickListener() {
			            public void onClick(View v){ 
			            	Spinner spn_ef1 = (Spinner)findViewById(R.id.spnEF1);
			            	Spinner spn_ef2 = (Spinner)findViewById(R.id.spnEF2);
			            	compararEF((String)spn_ef1.getSelectedItem(), (String)spn_ef2.getSelectedItem());
			            }		            
			        });
	}
	
	public void onItemSelected(AdapterView<?> arg0, View view,int position,long id) {
		if (arg0.getId()==R.id.spnTipoEF1){
			Spinner auxSpinner=(Spinner)arg0;			
			String nombreTipoEF= nomTipoEF.get(position);
        	posicion1 = buscaTipoEF(nombreTipoEF);
        	idTipoEF1= tipoef.get(posicion1).getId();
			
			//llenar rubros en un arreglo
        	ef1=llenaEF(idTipoEF1);
        	final ArrayList<String> nomEF1 = new ArrayList<String>();
        	for(int i=0; i<ef1.size();i++){
  			nomEF1.add(ef1.get(i).getRazonsocial());
        	}
        	String nombreEF1 = nomEF1.get(position);
        	posicion2 = buscaEF(nombreEF1, ef1);
        	idEF1 = ef1.get(position).getId();
        			
  		//llenar el spinner con ese arreglo
        	Spinner auxSpinner2 = (Spinner)this.findViewById(R.id.spnEF1);
        	ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, nomEF1);		
        	adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        	auxSpinner2.setAdapter(adapter2);        	
		}
		if (arg0.getId()==R.id.spnTipoEF2){
			Spinner auxSpinner3=(Spinner)arg0;			
			String nombreTipoEF2= nomTipoEF.get(position);
        	posicion3 = buscaTipoEF(nombreTipoEF2);
        	idTipoEF2= tipoef.get(posicion3).getId();
			
			//llenar rubros en un arreglo
        	ef2=llenaEF(idTipoEF2);
        	final ArrayList<String> nomEF2 = new ArrayList<String>();
        	for(int i=0; i<ef2.size();i++){
  			nomEF2.add(ef2.get(i).getRazonsocial());
        	}
        	String nombreEF2 = nomEF2.get(position);
        	posicion4 = buscaEF(nombreEF2, ef2);
        	idEF2 = ef2.get(position).getId();
        			
  		//llenar el spinner con ese arreglo
        	Spinner auxSpinner4 = (Spinner)this.findViewById(R.id.spnEF2);
        	ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, nomEF2);		
        	adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        	auxSpinner4.setAdapter(adapter3);        	
		}
		if (arg0.getId()==R.id.spnEF1){
			Spinner auxSpinner4=(Spinner)arg0;			
			String nombreEF1 = (String)auxSpinner4.getSelectedItem();
        	posicion2 = buscaEF(nombreEF1, ef1);
        	idEF1 = ef1.get(position).getId();
		}
		if (arg0.getId()==R.id.spnEF2){
			Spinner auxSpinner5=(Spinner)arg0;			
			//String nombreEF2 = nomEF2.get(position);
			String nombreEF2 = (String)auxSpinner5.getSelectedItem();
        	posicion4 = buscaEF(nombreEF2, ef2);
        	idEF2 = ef2.get(position).getId();
		}
	}
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	protected void compararEF(String ef1, String ef2) {
		EditText etmonto = (EditText)findViewById(R.id.etMontoCP);
        monto = Double.parseDouble(etmonto.getText().toString());
        String auxmonto = monto + "";
        EditText etcuota = (EditText)findViewById(R.id.etCuotaCP);
        cuota = Integer.parseInt(etcuota.getText().toString());
        String auxcuota = cuota + "";
                
		try {   
			dblnum1 = (new JSONParser_Final2().execute("http://10.0.2.2:3000/tasas/calcularCuotaIgual/"+idEF1+"/"+(auxmonto).replace('.', ',')+"/"+cuota+".json")).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		try {   
			dblnum2 = (new JSONParser_Final2().execute("http://10.0.2.2:3000/tasas/calcularCuotaIgual/"+idEF2+"/"+(auxmonto).replace('.', ',')+"/"+cuota+".json")).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		Intent intent = new Intent(this,comparacion.class);
		intent.putExtra("idUsuario",idUsuario);
		intent.putExtra("idEF1",idEF1);
		intent.putExtra("idEF2",idEF2);
		intent.putExtra("nombreEF1",ef1);
		intent.putExtra("nombreEF2",ef2);
		intent.putExtra("cuotaEF1",dblnum1);
		intent.putExtra("cuotaEF2",dblnum2);
		intent.putExtra("cuota",cuota);
		intent.putExtra("monto",auxmonto);
		startActivity(intent);
		
	}
	
	
	protected int buscaTipoEF(String nombreTipoEF) {
		int pos = 0;
		for (int i=0; i<tipoef.size(); i++){
			if (tipoef.get(i).getNombre().equals(nombreTipoEF)){
				pos=i;
				break;
			}
		}
		return pos;
	}

	protected int buscaEF(String nombreEF, ArrayList<EF> ef) {
		int pos = 0;
		for (int i=0; i<ef.size(); i++){
			if (ef.get(i).getRazonsocial().equals(nombreEF)){
				pos=i;
				break;
			}
		}
		return pos;
	}
	
	private ArrayList<tipoEF> llenaTipoEF() {
		ArrayList<tipoEF> tipos = new ArrayList<tipoEF>();
		JSONArray  jArray = null;
		try {   
			jArray = (new JSONParser_Final().execute("http://10.0.2.2:3000/tipo_efs/verTipoEF/1.json")).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		try {
			for(int i = 0; i < jArray.length(); i++){
				 JSONObject jsonItem = jArray.getJSONObject(i);
		         JSONObject jsonItem1 = jsonItem.getJSONObject("tipo_ef");
		         
		         int id = jsonItem1.getInt("id");
		         String nombre = jsonItem1.getString("nombre");
		         
		         tipoEF tipo = new tipoEF(id, nombre);
		         tipos.add(tipo);
		        
			}
		}catch (JSONException e) {
		     e.printStackTrace();
		 }
		return tipos;
	}
	private ArrayList<EF> llenaEF(int idTipoEF) {
		ArrayList<EF> efs = new ArrayList<EF>();
		JSONArray  jArray = null;
		try {   
			jArray = (new JSONParser_Final().execute("http://10.0.2.2:3000/efs/verEF/"+idTipoEF+".json")).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		try {
			for(int i = 0; i < jArray.length(); i++){
				 JSONObject jsonItem = jArray.getJSONObject(i);
		         JSONObject jsonItem1 = jsonItem.getJSONObject("ef");
		         
		         int id = jsonItem1.getInt("id");
		         String razonsocial = jsonItem1.getString("razonsocial");
		         
		         EF ef = new EF(id, razonsocial);
		         efs.add(ef);
		        
			}
		}catch (JSONException e) {
		     e.printStackTrace();
		 }
		return efs;
	}
}
